package com.xmg.mgrsite.base.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xmg.p2p.base.domain.Logininfo;
import com.xmg.p2p.base.service.ILogininfoService;
import com.xmg.p2p.base.util.JSONResult;

@Controller
public class LoginfoController {
	@Autowired
	private ILogininfoService logininfoService;
	
	@RequestMapping("login")@ResponseBody
	public JSONResult login(String username, String password, HttpServletRequest request) {
		JSONResult result = new JSONResult();

		try {
			String ip =request.getRemoteAddr();
			Logininfo login = logininfoService.login(username, password, ip,Logininfo.USER_MANAGER);

			if(login==null){
				result.setSuccess(false);
				result.setMsg("用户账号密码错误!");
			}else{

				result.setMsg("登录成功");
			}
		}catch(Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg("用户登录失败,原因:"+e.getMessage());
		}
        //Logininfo current = UserContext.getCurrent();
        //System.out.println(current.getUsername());
		return result;

	}
	@RequestMapping("index")
	public String index() {
		return "main";
	}

}
